package com.briup.cms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.briup.cms.domain.User;
import com.briup.cms.domain.vo.UserVo;
import com.briup.cms.service.IUserService;
import com.briup.cms.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author briup
 * @since 2023-03-09
 */
@Api("用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @ApiOperation("新增或修改用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "新增或修改的用户")
    })
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody User user){
        userService.saveOrUpdate(user);
        return Result.success();
    }

    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id")
    })
    @GetMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        userService.deleteById(id);
        return Result.success();
    }

    @ApiOperation("根据id查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id")
    })
    @GetMapping("/queryById")
    public Result queryById(Long id){
        User user = userService.queryById(id);
        return Result.success(user);
    }

    @ApiOperation("条件查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码"),
            @ApiImplicitParam(name = "pageSize",value = "每页大小"),
            @ApiImplicitParam(name = "roleId",value = "角色类型"),
            @ApiImplicitParam(name = "username",value = "角色名称"),
            @ApiImplicitParam(name = "status",value = "状态")
    })
    @PostMapping("/query")
    public Result query(int pageNum, int pageSize,
                        Integer roleId, String username,
                        String status){
        IPage<UserVo> query = userService.query(pageNum, pageSize, roleId, username, status);
        return Result.success(query);
    }

    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名"),
            @ApiImplicitParam(name = "password",value = "密码")
    })
    @PostMapping("/login")
    public Result login(String username,String password){
        String token = userService.login(username, password);
        return Result.success(token);
    }

}

